package com.getinstaliker.screen;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.getinstaliker.activity.MainFragmentActivity;
import com.getinstaliker.httptask.GetInstalikerTask;
import com.getinstaliker.httptask.model.MediaIdModel;
import com.getinstaliker.httptask.model.ResultModel;
import com.getinstaliker.httptask.model.UpdateLikeModel;
import com.getinstaliker.httptask.model.UserInfoModel;
import com.getinstaliker.instagram.InstagramBaseTask.InstagramTaskListener;
import com.getinstaliker.instagram.InstagramUtil;
import com.getinstaliker.instagram.SessionStore;
import com.getinstaliker.instagram.model.MediaImageModel;
import com.getinstaliker.instagram.model.MediaImageModel.ImageData;
import com.getinstaliker.instagram.model.MediaImageModel.ImageInfo;
import com.getinstaliker.instagram.model.MediaLikesModel;
import com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment;
import com.getinstaliker.util.DialogUtil;
import com.getinstaliker.util.InstalikerCoinsController;
import com.getinstaliker.util.DialogUtil.DialogListener;
import com.getinstaliker.util.InstalikerDialogInfo;
import com.getinstaliker.util.UniversalImageLoader;
import com.instagram.getinstaliker.R;

// TODO: Auto-generated Javadoc
/**
 * The Class GetCoinsFragment.
 */
public class GetCoinsFragment extends BaseGetInstalikerHeaderFragment implements
		OnClickListener {

	/** The img photo. */
	private ImageView imgPhoto;

	/** The m get coins controler. */
	private GetCoinsControler mGetCoinsControler;
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.getinstaliker.screen.common.BaseFragmentScreen#onCreateContentView
	 * (android.view.LayoutInflater, android.view.ViewGroup)
	 */
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View vContent = inflater.inflate(R.layout.get_coins_screen, container,false);
		vContent.findViewById(R.id.imgWraper).setLayoutParams(new LinearLayout.LayoutParams
										(LayoutParams.MATCH_PARENT,MainFragmentActivity.SCREEN_WIDTH));
		init(vContent);
		return vContent;
	}

	/**
	 * Inits the.
	 * 
	 * @param vContent
	 *            the v content
	 */
	public void init(View vContent) {
		imgPhoto 			= (ImageView) vContent.findViewById(R.id.imgPhoto);
		mGetCoinsControler 	= new GetCoinsControler();
		vContent.findViewById(R.id.btnSkip).setOnClickListener(this);
		vContent.findViewById(R.id.btnLike).setOnClickListener(this);
		vContent.findViewById(R.id.btnGetProOrFreeCoins).setOnClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#getTitleResId
	 * ()
	 */
	@Override
	protected int getTitleResId() {
		return R.string.get_coins;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSkip:
			mGetCoinsControler.showNewPhotoTask();
			break;
		case R.id.btnLike:
			mGetCoinsControler.likeTask();
			break;
		default:
			break;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#onReload
	 * ()
	 */
	@Override
	public void onReload() {
		mGetCoinsControler.showNewPhotoTask();
	}
	
	

	/**
	 * The Class GetCoinsControler.
	 */
	public class GetCoinsControler {
		/** The m current image model. */
		private MediaIdModel mCurrentImageModel = null;
		
		/**
		 * Instantiates a new gets the coins controler.
		 */
		public GetCoinsControler() {
			addUserTask();
		}
		
		/**
		 * Adds the user task.
		 */
		public void addUserTask() {
			GetInstalikerTask.addUser(getActivity(), SessionStore.getInstance().getInstaAccessToken(),
					new InstagramTaskListener<ResultModel>() {
						@Override
						public void onTaskComplete(ResultModel ob) {
							if (ob != null) {
								GetInstalikerTask.getUserInfo(getActivity(), SessionStore.getInstance().getInstaAccessToken(),new InstagramTaskListener<UserInfoModel>() {
										@Override
										public void onTaskComplete(UserInfoModel ob) {
											if (ob != null && ob.status.equals("OK")&& ob.data != null) {
												SessionStore.getInstance().saveUserCoins(ob.data.get(0).coins);
												updateCoins();
											}
											showNewPhotoTask();
										}
									});
							}
						}
					});
		}

		/**
		 * Show new photo.
		 */
		public void showNewPhotoTask() {
			GetInstalikerTask.getMediaIdTask(getActivity(), SessionStore
					.getInstance().getInstaAccessToken(),
					new InstagramTaskListener<MediaIdModel>() {

						@Override
						public void onTaskComplete(MediaIdModel model) {
							if (model != null && model.data != null&& model.data.media_id != null) {
								mCurrentImageModel = model;
								showImage(model.data.media_id);
							} else {
								mCurrentImageModel = null;
							}
						}
					});
		}

		/**
		 * Gets the image url.
		 * 
		 * @param mediaId
		 *            the media id
		 * @return the image url
		 */
		public void showImage(String mediaId) {
			InstagramUtil.getUrlImageTask(getActivity(), mediaId,
					new InstagramTaskListener<MediaImageModel>() {

						@Override
						public void onTaskComplete(MediaImageModel model) {
							if (model != null && model.data != null&& model.data.images != null&& model.data.images != null) {
								MediaImageModel.ImageInfo imageInfo = chooseResolution(model.data.images);
								UniversalImageLoader.getInstance().loadImageView(
												imgPhoto,
												imageInfo.url,
												Integer.valueOf(imageInfo.width),
												Integer.valueOf(imageInfo.height));
							}
						}
					});
		}

		/**
		 * Post like.
		 */
		public void likeTask() {
			if (mCurrentImageModel != null) {
				InstagramUtil.getMediaLikeTask(getActivity(),
						mCurrentImageModel.data.media_id,
						new InstagramTaskListener<MediaLikesModel>() {

							@Override
							public void onTaskComplete(MediaLikesModel ob) {
								if (ob != null && ob.meta.code == 200) {
									updateLikeSuccess();
								}
							}
						});
			}
		}

		/**
		 * Update like success.
		 */
		public void updateLikeSuccess() {
			GetInstalikerTask.updateLikeSucessTask(getActivity(),
					mCurrentImageModel, SessionStore.getInstance().getInstaAccessToken(),
					new InstagramTaskListener<UpdateLikeModel>() {
						@Override
						public void onTaskComplete(UpdateLikeModel ob) {
							if (ob != null && ob.status != null&& ob.status.equals("OK")) {
								InstalikerCoinsController.saveMoreCoins(2);
								updateCoins();
								DialogUtil.showConfirmDialog(getActivity(),InstalikerDialogInfo.TITLE_MESSAGE,InstalikerDialogInfo.MESSAGE_LIKE_SUCCESS,new DialogListener() {

											@Override
											public void onCancel() {}

											@Override
											public void onAccept(String result) {
												showNewPhotoTask();
											}
										});
							}
						}
					});
		}

		/**
		 * Choose resolution.
		 * 
		 * @param imageData
		 *            the image data
		 * @return the image info
		 */
		public ImageInfo chooseResolution(ImageData imageData) {
			int screenWidth = MainFragmentActivity.SCREEN_WIDTH;
			if (Integer.valueOf(imageData.standard_resolution.width) < screenWidth) {
				return imageData.standard_resolution;
			} else if (Integer.valueOf(imageData.low_resolution.width) < screenWidth) {
				return imageData.low_resolution;
			} else {
				return imageData.thumbnail;
			}
		}
	}

}
